package View;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.KeyEvent;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;

import Model.BusinessRule;
import Model.BusinessRuleManager;
import Model.Content.LanguageList;
import Model.Content.RuleType;
import Model.Content.RuleTypeList;

@SuppressWarnings("serial")
public class MyFrame extends JFrame
{
	private JFrame myframe;

	private JPanel centerPanel;
	private JPanel bottomPanel;
	private BusinessRuleManager BRList;

	private SelectBusinessRules generateList;
	private SelectBusinessRules selectList;
	private JComboBox selectLanguage;

	private ArrayList<String> ruleTypes;
	private JButton closeB;
	private JButton genererenB;
	private JButton selectedToGenerateB;
	private JButton generateToSelectedB;
	private JButton allToGenerateB;
	private JButton allToSelectedB;
	private JLabel pleaseWaitLabel = new JLabel("Receiving Business Rules");
	
	private JMenuBar menuBar;
	private JMenu file_menu, database_menu, about_menu;
	private JMenuItem menuItem_close, menuItem_setDatabase, menuItem_info;
	
	public MyFrame(BusinessRuleManager BRL)
	{
		BRList = BRL;
		new JFrame();
		myframe = this;
		closeB = new JButton("Close");
		genererenB = new JButton("Generate");
		selectedToGenerateB = new JButton(">");
		generateToSelectedB = new JButton("<");
		allToGenerateB = new JButton(">>");
		allToSelectedB = new JButton("<<");
		startingUP();
	}
	
	public void startingUP(){
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		setSize(165, 75);
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		setLocation((screenWidth / 2) - (getWidth() / 2), (screenHeight / 2) - (getHeight() / 2));
		add(pleaseWaitLabel);
		setVisible(true);
	}
	
	public void init()
	{
		myframe.remove(pleaseWaitLabel);
		setIconImage(getImage("Image/icon.png"));

		setSize(700,600);
		
		Toolkit tk = Toolkit.getDefaultToolkit();
		Dimension screenSize = tk.getScreenSize();
		int screenHeight = screenSize.height;
		int screenWidth = screenSize.width;
		setLocation((screenWidth / 2) - (getWidth() / 2), (screenHeight / 2) - (getHeight() / 2));

		
		
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		//pack();
		setTitle("Business Rule Generator");		
		
		
		//MenuBar config...
		menuBar = new JMenuBar();
		
		file_menu = new JMenu("File");
		file_menu.setMnemonic(KeyEvent.VK_F);
		file_menu.getAccessibleContext().setAccessibleDescription("Menu for doing basic window operations");
		
		menuItem_close = new JMenuItem("Close", KeyEvent.VK_C);
		menuItem_close.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_F4, ActionEvent.ALT_MASK));
		file_menu.add(menuItem_close);
		
		menuBar.add(file_menu);
		
		database_menu = new JMenu("Database");
		database_menu.setMnemonic(KeyEvent.VK_D);
		database_menu.getAccessibleContext().setAccessibleDescription("Menu for setting database connection");
		
		menuItem_setDatabase = new JMenuItem("Set Target Database", KeyEvent.VK_D);
		menuItem_setDatabase.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_D, ActionEvent.ALT_MASK));
		database_menu.add(menuItem_setDatabase);
		
		menuBar.add(database_menu);
		
		about_menu = new JMenu("About");
		about_menu.setMnemonic(KeyEvent.VK_A);
		about_menu.getAccessibleContext().setAccessibleDescription("About this software");
		
		menuItem_info = new JMenuItem("Info", KeyEvent.VK_I);
		menuItem_info.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_I, ActionEvent.ALT_MASK));
		about_menu.add(menuItem_info);
		
		menuBar.add(about_menu);
		
		myframe.setJMenuBar(menuBar);
		
		
		//MenuBar config end...
		
		setLayout(new BorderLayout());
		
		ruleTypes = new ArrayList<String>();
		ruleTypes.add("Alle Business Rules");
		for(RuleType t : RuleTypeList.getRuleTypeList()){
			ruleTypes.add(t.getName());
		}
				
		
		selectList = new SelectBusinessRules("SELECTING", BRList, ruleTypes.toArray());
		generateList = new SelectBusinessRules("GENERATING", BRList, ruleTypes.toArray());
		
		addPanelNorth();
		myframe.getContentPane().add(selectList, BorderLayout.WEST);
		myframe.getContentPane().add(generateList, BorderLayout.EAST);
		addPanelCenter();
		addPanelSouth();
		
		setVisible(true);
	}
	
	private void addPanelNorth(){
		
		myframe.getContentPane().add(new JLabel("Business Rule Generator"), BorderLayout.NORTH);
	}
	
	private void addPanelCenter(){
		centerPanel = new JPanel();
		myframe.getContentPane().add(centerPanel, BorderLayout.CENTER);
		centerPanel.setSize(new Dimension(100, 500));
		
		JPanel knoppenPanel = new JPanel();
		knoppenPanel.setSize(new Dimension(50,500));
		knoppenPanel.setLayout(new BoxLayout(knoppenPanel, BoxLayout.Y_AXIS));

		
		
		knoppenPanel.add(selectedToGenerateB);
		knoppenPanel.add(new JPanel());
		knoppenPanel.add(generateToSelectedB);
		knoppenPanel.add(new JPanel());
		knoppenPanel.add(allToGenerateB);
		knoppenPanel.add(new JPanel());
		knoppenPanel.add(allToSelectedB);
		
		centerPanel.add(knoppenPanel);
		selectedToGenerateB.setAlignmentX(CENTER_ALIGNMENT);
		generateToSelectedB.setAlignmentX(CENTER_ALIGNMENT);
		allToGenerateB.setAlignmentX(CENTER_ALIGNMENT);
		allToSelectedB.setAlignmentX(CENTER_ALIGNMENT);
		allToSelectedB.setAlignmentY(Container.BOTTOM_ALIGNMENT);
		//centerPanel.add(knoppenPanel, BorderLayout.CENTER);
		
		
	}
	
	private void addPanelSouth(){
		bottomPanel = new JPanel();
				
		selectLanguage = new JComboBox(LanguageList.getAllLanguages().toArray());
		bottomPanel.add(selectLanguage);
		
		
		bottomPanel.add(genererenB);
		bottomPanel.add(closeB);
		myframe.getContentPane().add(bottomPanel, BorderLayout.SOUTH);
	}
	
	private Image getImage(String path){
		  Image image = null;
			try {
				image = ImageIO.read(new File(path));
			} catch (IOException e) {
				e.printStackTrace();
			}
		return image;
	}
	
	public void addCloseListener(ActionListener Eli){
		closeB.addActionListener(Eli);
	}
	
	public void addGenerateListener(ActionListener Eli){
		genererenB.addActionListener(Eli);
	}
	
	public void addSelectedToGenerateBListener(ActionListener Eli){
		selectedToGenerateB.addActionListener(Eli);
	}
	
	public void addGenerateToSelectedBListener(ActionListener Eli){
		generateToSelectedB.addActionListener(Eli);
	}
	
	public void addAllToSelectedBListener(ActionListener Eli){
		allToSelectedB.addActionListener(Eli);
	}
	
	public void addAllToGenerateBListener(ActionListener Eli){
		allToGenerateB.addActionListener(Eli);
	}
	
	public BusinessRule getGeselecteerdeSelected(){
		return selectList.getGeselecteerdeSelected();
	}
	
	public BusinessRule getGeselecteerdeGenerate(){
		return generateList.getGeselecteerdeSelected();
	}

	public String getGenerateLanguage(){
		return selectLanguage.getSelectedItem().toString();
	}



	
}
